/*
 * @Author: MrDotYan 2564217790@qq.com
 * @Date: 2024-09-19 16:29:51
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-25 14:20:52
 * @FilePath: /webServerAdmin/src/stores/modules/socket.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from "pinia";
import { useSocket } from "@/hooks/useSocket";
import { SocketStore } from "../interface";
import { ElNotification } from "element-plus";

export const useSocketStore = defineStore({
  id: "Midway-socket",
  state: (): SocketStore => {
    return {
      globalSocket: undefined
    };
  },
  actions: {
    disconnectGlobalSocket() {
      this.globalSocket?.disconnect();
    },
    connectGlobalSocket() {
      this.globalSocket = useSocket("/global");
      this.globalSocket.on("GLOBAL_MESSAGE", msg => {
        console.log(msg);
      });
      this.globalSocket.on("disconnect", () => {
        ElNotification.error("即时通讯连接已断开!");
      });
      this.globalSocket.on("onConnected", r => {
        const { code, message } = r;
        if (code === 200) {
          ElNotification.success("即时通讯连接成功!");
        } else {
          ElNotification.error(message);
        }
      });
    }
  }
});
